<?php
/*
Plugin Name: Multi-user plugin Manager
Plugin URI: http://samson.blog.asu.edu/plugman
Description:  Allows Management of plugins for users
Author: Brian Samson
Version: 1.0
Author URI: http://samson.blog.asu.edu
*/

Class MU_Plugin_Manager {

   var $baseurl;

   function MU_Plugin_Manager() {
       add_action('admin_menu', array(&$this, 'plugins_admin_menu'));
   }

   function plugin_content() { 
    global $wpdb;
     
     if( !is_site_admin() )
	      return;
  
     $this->baseurl = "?page=" . $_GET['page'];
    
        ?><div class="wrap"><h2>MU Plugin manager.</h2><?php

     $this->main_menu();
     switch ( $_GET['action'] ) {
        case 'deactivate':
           if (!isset($_GET['blog']) && !isset($_GET['plugin']) ) 
             echo "<p>quit messing around and just click links please</p>"; 
           else
             $this->deactivate_plugin(trim($_GET['plugin']), $_GET['blog']);
           break;

        case 'activate': 
           if (!isset($_GET['blog']) && !isset($_GET['plugin']) ) 
             echo "<p>quit messing around and just click links please</p>"; 
           else
             $this->activate_plugin(trim($_GET['plugin']), $_GET['blog']);
           break;
        
     }
     switch ($_GET['view']) {
        case 'check': 
           $this->check_plugins();
           break;

        case 'detail':
          if (!isset($_GET['blog'])) 
            echo "<p>no blog, try again</p>";
          else
            $this->blog_detail($_GET['blog']);
          break;

        default: 
        ?>
          <h3>Welcome to the MU Plugin manager</h3>
          <p>Through this page you can globally manipulate the active plugins for individual blogs.</p>
        <?php
        break;
     }

     echo "</div>";
   }
  
  function plugins_admin_menu() {
          $pfile = basename(dirname(__FILE__)) . '/' . basename(__FILE__);
          if ( is_site_admin() )
                  add_submenu_page('wpmu-admin.php', 'Plugins', 'Plugins', 0, $pfile, array(&$this, 'plugin_content'));
  }

  function main_menu() {
  ?>
          <div style="float:right; margin-right: 30px; border: 2px solid maroon; padding: 5px; top: -40px; position: relative;">
          <b>Quick Menu:</b>
          <ul>
              <li><a href="<?php echo $this->baseurl?>&view=check">Check active plugins</a></li>
              <li><a href="<?php echo $this->baseurl?>&view=detail&blog=0">Global Management</a></li>
          </ul>
          </div>
  <?php
  } 
  function check_plugins() {
     global $wpdb;

     ?><h3>Global Plugin list</h3>
       <div style="margin-left: 25px; width: 60%" >This is a list of all activated plugins for all blogs on the system.
          You can click on an active plugin to deactive it. 
          You can click on a blog's ID number to view/perform detailed maintenance on that blog.  
          Please scroll to the bottom to select another action.</div>
     <?php

     $query = "select blog_id from {$wpdb->blogs}";
     $bloglist = $wpdb->get_results($query);
     print '<table style="margin: 20px auto;" id="the-list-x" width="100%" cellpadding="3" cellspacing="3">';
     print "<tr><th>Blog id</th><th>Blog Name</th><th>active plugins</th></tr>";
     $alt="alternate";
     foreach ($bloglist as $row)  {
        $plugins = get_blog_option($row->blog_id, "active_plugins");
        $bloginfo = get_blog_details($row->blog_id);
        print "<tr class='$alt'><td><a href='{$this->baseurl}&view=detail&blog={$bloginfo->blog_id}'>{$bloginfo->blog_id}</a></td>
               <td><a href='{$bloginfo->siteurl}'>{$bloginfo->blogname}</a></td><td>";
        if (is_array($plugins) && sizeof($plugins) > 0) {
          foreach ( $plugins as $id => $plugin ) {
              print "<a href='{$this->baseurl}&action=deactivate&blog={$bloginfo->blog_id}&plugin=$plugin&view=check'>$plugin</a>&nbsp;";
          }
        } else {
          print "No active plugins";
        }
        print "</td></tr>";
        $this->newrow($alt);      
     }
     print "</table>";
  }
  function newrow(&$a) {
    if ($a == "alternate") 
       $a = "";
    else 
       $a = "alternate";
  }

  function blog_detail($blogid) {
    if ($blogid == 0) 
      return $this->global_detail();
    $bloginfo = get_blog_details($blogid);
    ?>
    <h3>Detailed plugin information for <?=$bloginfo->blogname ?> (ID# <?=$bloginfo->blog_id ?>)</h3>
    <br/>
    <p>Active plugins for this blog:</p>
    <?php
    $active = get_blog_option($bloginfo->blog_id, "active_plugins");
    $available = $this->get_available_plugins($blogid);
    if (is_array($available)) {
      echo "<table cellpadding='3' cellspacing='3' style='width:100%; margin: 20px auto;'>"; 
      echo "<tr><th>Plugin</th><th>Version</th><th>Description</th><th>Action</th></tr>";
      $alternate = 'alternate';
      foreach ($available as $file => $plugin) {
        $act = '';
        $actionlink = "<a href='{$this->baseurl}&action=activate&blog=$blogid&plugin=$file&view=detail'>Activate</a>";
        if (in_array($file, $active)) {
          $act = 'active'; 
          $actionlink = "<a href='{$this->baseurl}&action=deactivate&blog=$blogid&plugin=$file&view=detail'>Deactivate</a>";
        }
        echo "<tr class='$alternate $act'>";
        echo "<td class='name'><strong>{$plugin['Title']}</td>
              <td>{$plugin['Version']}</td>
              <td>{$plugin['Description']}</td>
              <td>$actionlink</td>";
        $this->newrow($alternate); 
      }
      echo "</table>";
    } else { 
      echo "There are no plugins available for this blog.";
    }
  }

  function global_detail() {
    ?><h3>Global plugin management</h3>
      <div style='width: 60%; margin-left: 25px'>
      This page can globally, i.e. for every blog on the site, activate or deactivate plugins.  Simply click on the links 
      below and to the right to enforce site-wide plugin activation policies.  The changes go into effect immediately.  
      </div> 
    <?php
    $available = $this->get_available_plugins(0);
    if (is_array($available)) {
      echo "<table cellpadding='3' cellspacing='3' style='width:100%; margin: 20px auto;'>";
      echo "<tr><th>Plugin</th><th>Version</th><th>Description</th><th>Activate</th><th>Deactivate</th></tr>";
      $alternate = 'alternate';
      foreach ($available as $file => $plugin) {
        echo "<tr class='$alternate $act'>";
        echo "<td class='name'><strong>{$plugin['Title']}</td>
              <td>{$plugin['Version']}</td>
              <td>{$plugin['Description']}</td>
              <td><a href='{$this->baseurl}&action=activate&blog=0&plugin=$file&view=detail'>Globally Activate</td>
              <td><a href='{$this->baseurl}&action=deactivate&blog=0&plugin=$file&view=detail'>Globally Deactivate</td>";
        $this->newrow($alternate);
      }
      echo "</table>";
    } else {
      echo "There are no plugins available for this blog.";
    }
  }

  function activate_plugin($plugin, $blogid = 0) {
    $query = "select blog_id from wp_blogs";
    if ($blogid != 0)  {
       $query = $query . " where blog_id = $blogid";
    }
    $res = mysql_query($query);
    while ( $row = mysql_fetch_array($res)) {
      $blogid = $row['blog_id'];
      $plugins = get_blog_option($blogid, "active_plugins");
      $bloginfo = get_blog_details($blogid);
      echo "<br/>Activating $plugin on {$bloginfo->blogname} ({$bloginfo->blog_id})...";
      if (is_array($plugins)) {
         if (!in_array($plugin, $plugins)) {
           $plugins[] = $plugin;
           sort($plugins);
         }else{
           echo "plugin already active!";
           continue; 
         }
      } else {
        //create an array and add it
        $plugins = array($plugin);
      }
      update_blog_option($blogid, "active_plugins", $plugins);
      echo "Done!";      
    }
  }

  function deactivate_plugin($plugin, $blogid = 0) {
    $query = "select blog_id from wp_blogs";
    if ($blogid != 0)  {
       $query = $query . " where blog_id = $blogid";
    }
    $res = mysql_query($query);
    while ( $row = mysql_fetch_array($res)) {
      $plugins = get_blog_option($row['blog_id'], "active_plugins");
      $bloginfo = get_blog_details($row['blog_id']);
      echo "<br/>Deactivating $plugin on {$bloginfo->blogname} ({$bloginfo->blog_id})...";
      if (is_array($plugins)) {
        array_splice($plugins, array_search( $plugin, $plugins), 1 ); // Array-fu!
        update_blog_option($blogid, "active_plugins", $plugins);
        echo "Done!";
      } else {
        echo "Blog has no active plugins!";
      } 
    }
  }

  function get_available_plugins($blogid) {
    global $wpdb;
    $current_id = $wpdb->blogid;
    $wpdb->blogid=$blogid;
    $plugins = get_plugins();
    $wpdb->blogid=$current_id;
    return $plugins;
  }


} 

$mu_plugin_man = new MU_Plugin_Manager();
?>
